package org.fastiot.data.sampling

class Threshold {

    static enum Operator {
        EQ(0), GE(1), GT(2), LT(3), LE(4)

        final int value

        private Operator(int value) { this.value = value }

        @Override
        String toString() {
            switch (value) {
                case EQ:
                    return "=="
                case GE:
                    return ">="
                case GT:
                    return ">"
                case LT:
                    return "<"
                case LE:
                    return "<="
            }
        }
    }

    static enum MatchingStrategy {
        Disable(0), WhiteList(1), BlackList(2)
        final int value

        private MatchingStrategy(int value) { this.value = value }
    }

    /**
     * 数据包标识（Hex）
     */
    String hexPacketCode
    /**
     * 数据包标识
     */
    Integer packetCode
    /**
     * 数据标识符
     */
    String dataIdentifier
    /**
     * 数据名称
     */
    String dataName
    /**
     * 告警提示语
     */
    String warningTips
    /**
     * 运算符
     */
    Operator operator
    /**
     * 阈值
     */
    BigDecimal value

    /**
     * 事件标识
     */
    String eventId

    /**
     * 事件级别
     */
    Short eventLevel

    /**
     * 识别间隔（秒）
     */
    Integer recognitionInterval

    /**
     * （终端）匹配策略
     */
    MatchingStrategy matchingStrategy
    /**
     * （终端）匹配名单，内容格式为多个终端代码且以半角逗号分隔。
     */
    String matchList

    boolean enabled = true
    String remark
    Date dateCreated
    Date lastUpdated

    static transients = ['hexPacketCode']

    static constraints = {
        hexPacketCode(blank: false, matches: /[0-9a-fA-F]+/)
        packetCode(nullable: false)
        dataIdentifier(blank: false, matches: /[a-zA-Z0-9]+/)
        dataName(blank: false)
        operator(blank: false, inList: Operator.values().toList())
        value(blank: false, unique: ["packetCode", "dataIdentifier", "operator", "matchingStrategy", "matchList"])
        recognitionInterval(nullable: false, min: 0)
        eventId(blank: false, unique: true)
        eventLevel(blank: false, range: 1..10)
        warningTips(blank: false)
        matchingStrategy(nullable: false, inList: MatchingStrategy.values().toList())
        matchList(nullable: true, matches: /\w+[,\w+]*/)
        enabled(blank: false)
        remark(nullable: true)
        dateCreated(display: false)
        lastUpdated(display: false)
    }

    static mapping = {
        sort packetCode: "asc"
        remark type: "text"
        matchList type: "text"
        packetCode index: ((Class) this).simpleName + "_PacketCode_DataCode_idx"
        dataIdentifier index: ((Class) this).simpleName + "_PacketCode_DataCode_idx"
        dateCreated index: ((Class) this).simpleName + "_DateCreated_idx"
    }

    def beforeValidate() {
        if (packetCode && !hexPacketCode)
            hexPacketCode = Integer.toHexString(packetCode)
        else if (hexPacketCode && !packetCode)
            packetCode = Integer.parseInt(hexPacketCode, 16)
    }

    def afterLoad() {
        hexPacketCode = Integer.toHexString(packetCode)
    }
}
